package team.scau.laboratory_repair.web.sys;

import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import team.scau.laboratory_repair.common.constant.ShiroConstant;
import team.scau.laboratory_repair.common.enums.LoginType;
import team.scau.laboratory_repair.common.utils.Md5Utils;
import team.scau.laboratory_repair.modules.po.User;
import team.scau.laboratory_repair.service.UserService;
import team.scau.laboratory_repair.common.shiro.UserToken;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 30254
 * creadtedate:2018/8/17
 */
@Controller
@RequestMapping("/pc")
@SessionAttributes(types = User.class,names = "User")
@CommonsLog
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping("/login.html")
    public String login(Model model, HttpServletRequest request) throws Exception {
        String loginPath = request.getParameter("loginPath");
        if (StringUtils.isEmpty(loginPath)) {
            loginPath = "/pc/main";
        }
        model.addAttribute("loginPath", loginPath);
        return "/pc/login";
    }

    @PostMapping("/login.html")
    public String login(User user, String code, Model model, HttpSession session
            , RedirectAttributes redirectAttributes, String loginPath) throws Exception {
        //处理验证码
        if (!(code.equalsIgnoreCase(session.getAttribute(ShiroConstant.REQUEST_CODE).toString()))) {
            redirectAttributes.addFlashAttribute("message", "验证码错误");
            return "redirect:/pc/login.html?loginPath=" + loginPath;
        }

        Subject current = SecurityUtils.getSubject();
        if(!current.isAuthenticated()){
            try {
                user.setPassword(new Md5Utils().getkeyBeanofStr(user.getPassword()).toUpperCase());
                UserToken userToken = new UserToken(user.getAccount(), user.getPassword(), LoginType.ADMIN_USER.toString());
                log.info("test123:" + userToken.getUsername() + "," + userToken.getPassword() + "," + userToken.getLoginType());
                current.login(userToken);
            } catch (Exception e) {
                //可以写跳转的处理异常的页面
                redirectAttributes.addFlashAttribute("message", "账号或者密码错误");
                return "redirect:/pc/login.html?loginPath=" + loginPath;
            }
        }
        User currentUser = userService.getByAccount(user.getAccount());
        user.setPassword(new Md5Utils().getkeyBeanofStr(user.getPassword()).toUpperCase());


        session.setAttribute(ShiroConstant.REQUEST_C_USER, currentUser);
        session.setAttribute(ShiroConstant.REQUEST_C_USER_ID, currentUser.getId());

        return "redirect:" + loginPath;
    }

    @GetMapping("/logout.html")
    public String logout(HttpSession session) throws Exception {
        try {
            SecurityUtils.getSubject().logout();
        }catch (Exception ex){
            ex.printStackTrace();
            log.error(ex.getMessage(), ex);
        }
        return "redirect:/pc/login.html";
    }
}
